import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/goodsModel.dart';

class RechargeBloc implements BlocBase {

  RechargeBloc() {
    goodsList();
  }

  // ##########  DEFAULT  ##############

  List<GoodsModel> rechargeDefault = List<GoodsModel>();

  // 当前选择的充值金额
  int rechargeIndex = 0;

  // ##########  STREAMS  ##############

  /// 充值stream
  StreamController _recharge = StreamController<List<GoodsModel>>.broadcast();
  Sink get _rechargeSink => _recharge.sink;
  Stream get rechargeStream => _recharge.stream;

  /// 充值金额索引stream
  StreamController _rechargeIndex = StreamController<int>.broadcast();
  Sink get _rechargeIndexSink => _rechargeIndex.sink;
  Stream get rechargeIndexStream => _rechargeIndex.stream;

  void dispose() {
    _recharge.close();
    _rechargeIndex.close();
  }

  // ##########  HANDLING  ##############

  /// 充值商品列表
  void goodsList() async {
    List<GoodsModel> result = await UserApis.goods();
    _rechargeSink.add(result);
  }

  /// 改变当前金额
  void changeGoldIndex(int i) {
    _rechargeIndexSink.add(i);
  }
}
